package freelinz.smart.gwtex.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class Spliter
{
    public static class SpliterPanel extends SimplePanel{

        protected boolean dragging = false;
        protected int startX;
        protected int startY;
        protected SpliterPanel()
        {
            super();
            sinkEvents(Event.MOUSEEVENTS);
            DOM.setStyleAttribute(getElement(), "cursor", "move");
        }
        public void onBrowserEvent(Event event)
        {
            super.onBrowserEvent(event);
            int x = DOM.eventGetClientX(event);
            int y = DOM.eventGetClientY(event);
            switch (DOM.eventGetType(event))
            {
            case Event.ONMOUSEDOWN:
                onMouseDown(this, x, y);
                break;
            case Event.ONMOUSEUP:
                onMouseUp(this, x, y);
                break;
            case Event.ONMOUSEMOVE:
                onMouseMove(this, x, y);
                break;
            }
        }
        protected void onMouseDown(Widget sender, int x, int y)
        {
            this.dragging = true;
            DOM.setCapture(this.getElement());
            this.startX = x;
            this.startY = y;
        }
        protected void onMouseUp(Widget sender, int x, int y)
        {
            if(this.dragging)
            {
                this.dragging = false;
                DOM.releaseCapture(this.getElement());
            }
        }
        protected void onMouseMove(Widget sender, int x, int y)
        {
        }
        public boolean isDragging()
        {
            return dragging;
        }
        public void setDragging(boolean dragging)
        {
            this.dragging = dragging;
        }
        public int getStartX()
        {
            return startX;
        }
        public void setStartX(int startX)
        {
            this.startX = startX;
        }
        public int getStartY()
        {
            return startY;
        }
        public void setStartY(int startY)
        {
            this.startY = startY;
        }
    }
}
